package com.geilizu.dom.controller;

import com.geilizu.dom.dto.BuildingDto;
import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.dto.UserInfo;
import com.geilizu.dom.entity.Dormitory;
import com.geilizu.dom.entity.DormitoryAdmin;
import com.geilizu.dom.result.ResponseCode;
import com.geilizu.dom.result.ResponseData;
import com.geilizu.dom.service.DormitoryAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (DormitoryAdmin)表控制层
 *
 * @author makejava
 * @since 2022-07-12 15:08:42
 */
@RestController
@RequestMapping("dormitoryAdmin")
public class DormitoryAdminController {
    /**
     * 服务对象
     */
    @Autowired
    private DormitoryAdminService dormitoryAdminService;

    @GetMapping("/login")
    public ResponseData login(UserInfo userInfo) {
        String username = userInfo.getUsername();
        String password = userInfo.getPassword();
        DormitoryAdmin dormitoryAdmin = dormitoryAdminService.login(username, password);
//        System.out.println(dormitoryAdmin);
        if (dormitoryAdmin == null) {
            return new ResponseData(ResponseCode.DATAERROR);
        } else {
            return new ResponseData(ResponseCode.SUCCESS, dormitoryAdmin);
        }
    }
    @PostMapping("/save")
    public ResponseData save(@RequestBody DormitoryAdmin dormitoryAdmin){
        boolean save = this.dormitoryAdminService.save(dormitoryAdmin);
        if(!save){
            return new ResponseData(ResponseCode.DATAERROR);
        }
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @GetMapping("/list")
    public ResponseData list(){
        List<DormitoryAdmin> dormitoryAdminList = dormitoryAdminService.list();

        return new ResponseData(ResponseCode.SUCCESS,dormitoryAdminList);
    }

    @GetMapping("/list/{page}/{size}")
    public ResponseData list(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Map<String,Object> map = this.dormitoryAdminService.list(page, size);

        return new ResponseData("0","success", (Long) map.get("count"), map.get("dormitoryAdminList"));
    }

    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchDto searchDto){
        Map<String,Object> map=dormitoryAdminService.search(searchDto);
        return new ResponseData("200","success", (Long) map.get("count"),map.get("doAdminList"));
    }

    @DeleteMapping("/deleteById/{id}")
    public ResponseData deleteById(@PathVariable("id") Integer id) {
        Integer del = this.dormitoryAdminService.deleteById(id);
        if(del>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.DATAERROR);
    }


    @PutMapping("/update")
    public ResponseData update(@RequestBody DormitoryAdmin dormitoryAdmin){
        Integer update = this.dormitoryAdminService.updateById(dormitoryAdmin);
        if(update>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FAILED);
        }

    }
    @GetMapping("/findById/{id}")
    public ResponseData findById(@PathVariable("id") Integer id) {
        DormitoryAdmin dormitoryAdmin = dormitoryAdminService.findById(id);
        return new ResponseData(ResponseCode.SUCCESS, dormitoryAdmin);
    }


}

